<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="130px">
      <el-form-item
        label="饲料种类名称"
        prop="name"
        :rules="[{ required:true,message:'请输入饲料种类名称',trigger:['blur','change'] }]"
      >
        <el-input v-model="editPram.name"  :maxlength="biztype.value === 1 ? 8 : 20" placeholder="请输入饲料种类名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')" v-hasPermi="['admin:category:update']">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<!--创建和编辑公用一个组件-->
<script>
import * as adoptFodder from '@/api/adoptFodder.js'
import * as selfUtil from '@/utils/ZBKJIutil.js'
export default {
  // name: "edit"
  props: {
    biztype: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        name: null
      },
      isCreate:0
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      console.log("携带",this.editPram);
      const { name } = this.editData
      if(this.isCreate === 1){
        this.editPram.name = name
      }else{
      }
    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      if (isSave) {
       // this.editPram.pid = this.prent.id
        this.loadingBtn = true
        adoptFodder.addAdoptFodder(this.editPram).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('创建饲料种类成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      } else {
        this.editPram.pid = Array.isArray(this.editPram.pid) ? this.editPram.pid[0] : this.editPram.pid
        this.loadingBtn = true
        adoptFodder.updateAdoptFodder(this.editPram).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('更新饲料种类成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
